package ${generator.packageName}.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import javax.validation.Valid;

import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import ${generator.commonPackage}.base.utils.DxracerParamPage;
import ${generator.commonPackage}.base.utils.DxracerResponse;
import ${generator.commonPackage}.base.utils.DxracerResponsePage;
import ${generator.commonPackage}.oauth.anno.LoginAuthorityRequired;
import ${generator.commonPackage}.oauth.anno.SysLog;

import ${generator.packageName}.entity${(generator.childPackageName)!""}.dto.${generator.className}InsertDTO;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.dto.${generator.className}QueryDTO;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.dto.${generator.className}UpdateDTO;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.vo.${generator.className}GetVO;
import ${generator.packageName}.service.${generator.className}Service;

/**
 * ${generator.tableComment} 前端控制器
 *
 * @author ${generator.author}
 * @version 0.0.1
 * @date ${.now?string["yyyy-MM-dd HH:mm:ss"]}
 * @since ${.now?string["yyyy-MM-dd HH:mm:ss"]}
 */
@Api(tags = "${generator.tableComment}")
@RestController
@RequestMapping("${generator.path}")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ${generator.className}Controller {

    private final ${generator.className}Service ${generator.className?uncap_first}Service;

<#if generator.functions?seq_contains("get")>
    @GetMapping
    @LoginAuthorityRequired(value = "${generator.permission}:get")
    @ApiOperation("获取一条数据")
    public DxracerResponse<${generator.className}GetVO> get(@Valid ${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        return ${generator.className?uncap_first}Service.selectOne(${generator.className?uncap_first}QueryDTO);
    }

    <#if relations?? && (relations?size > 0)>
    @GetMapping(value = "/relation")
    @LoginAuthorityRequired(value = "${generator.permission}:relationGet")
    @ApiOperation("联表查询一条数据")
    public DxracerResponse<${generator.className}GetVO> getRelation(@Valid ${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        return ${generator.className?uncap_first}Service.selectRelation(${generator.className?uncap_first}QueryDTO);
    }

    </#if>
</#if>
<#if generator.functions?seq_contains("page")>
    @GetMapping(value = "/page")
    @LoginAuthorityRequired(value = "${generator.permission}:page")
    @ApiOperation("分页查询")
    public DxracerResponsePage<${generator.className}GetVO> getPage(@Valid ${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO,
                                                         @Valid DxracerParamPage dxracerParamPage) {
        return ${generator.className?uncap_first}Service.getPage(${generator.className?uncap_first}QueryDTO, dxracerParamPage);
    }

    <#if relations?? && (relations?size > 0)>
    @GetMapping(value = "/relation/page")
    @LoginAuthorityRequired(value = "${generator.permission}:relationPage")
    @ApiOperation("联表分页查询")
    public DxracerResponsePage<${generator.className}GetVO> getRelationPage(@Valid ${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO,
                                                                    @Valid DxracerParamPage dxracerParamPage) {
        return ${generator.className?uncap_first}Service.getRelationPage(${generator.className?uncap_first}QueryDTO, dxracerParamPage);
    }

    </#if>
</#if>
<#if generator.functions?seq_contains("list")>
    @GetMapping(value = "/list")
    @LoginAuthorityRequired(value = "${generator.permission}:list")
    @ApiOperation("查询列表")
    public DxracerResponse<List<${generator.className}GetVO>> getList(@Valid ${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        return ${generator.className?uncap_first}Service.getList(${generator.className?uncap_first}QueryDTO);
    }

    <#if relations?? && (relations?size > 0)>
    @GetMapping(value = "/relation/list")
    @LoginAuthorityRequired(value = "${generator.permission}:relationList")
    @ApiOperation("联表分页查询")
    public DxracerResponse<List<${generator.className}GetVO>> getRelationList(@Valid ${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        return ${generator.className?uncap_first}Service.getRelationList(${generator.className?uncap_first}QueryDTO);
    }

    </#if>
</#if>
<#if generator.functions?seq_contains("insert")>
    @PostMapping
    @LoginAuthorityRequired(value = "${generator.permission}:insert")
    @ApiOperation("新增")
    @SysLog(value = "新增")
    public DxracerResponse<String> insert(@RequestBody @Valid ${generator.className}InsertDTO ${generator.className?uncap_first}InsertDTO) {
        return ${generator.className?uncap_first}Service.insert(${generator.className?uncap_first}InsertDTO);
    }

</#if>
<#if generator.functions?seq_contains("update")>
    @PutMapping
    @LoginAuthorityRequired(value = "${generator.permission}:update")
    @ApiOperation("修改")
    @SysLog(value = "修改")
    public DxracerResponse<String> update(@RequestBody @Valid ${generator.className}UpdateDTO ${generator.className?uncap_first}UpdateDTO) {
        return ${generator.className?uncap_first}Service.modify(${generator.className?uncap_first}UpdateDTO);
    }

</#if>
<#if generator.functions?seq_contains("delete")>
    @DeleteMapping(value = "/{id}")
    @LoginAuthorityRequired(value = "${generator.permission}:delete")
    @ApiOperation("删除")
    @SysLog(value = "删除")
    public DxracerResponse<String> deleteById(@PathVariable Integer id) {
        return ${generator.className?uncap_first}Service.removeById(id) ? DxracerResponse.success("删除成功") : DxracerResponse.error(500, "删除失败");
    }

</#if>
<#if generator.functions?seq_contains("batchInsert")>

</#if>
<#if generator.functions?seq_contains("batchUpdate")>

</#if>
<#if generator.functions?seq_contains("batchDelete")>

</#if>
<#if generator.functions?seq_contains("import")>

</#if>
<#if generator.functions?seq_contains("export")>

</#if>
}
